package com.example.inventoryservice.company.strategy;


import com.example.inventoryservice.company.CompanyFinancialData;
import com.example.inventoryservice.company.IncomeTaxStrategy;
import com.example.inventoryservice.company.TaxCalculationResult;

import java.math.BigDecimal;

public class HighTechEnterpriseTaxStrategy implements IncomeTaxStrategy {
    @Override
    public TaxCalculationResult calculate(CompanyFinancialData data) {
        // 高新技术企业适用15%税率
        BigDecimal taxableIncome = data.getTaxableIncome();
        BigDecimal taxRate = new BigDecimal("0.15");
        BigDecimal baseTax = taxableIncome.multiply(taxRate);

        // 研发费用加计扣除: 研发费用的175%加计扣除
        BigDecimal rndDeduction = calculateRndDeduction(data);
        BigDecimal deductions = rndDeduction.multiply(taxRate);

        BigDecimal finalTax = baseTax.subtract(deductions);
        BigDecimal prepaidTax = data.getQuarterlyTaxPrepayments().stream().reduce(BigDecimal.ZERO, BigDecimal::add);

        return new TaxCalculationResult(taxableIncome, taxRate, baseTax, deductions, finalTax, prepaidTax);
    }

    private BigDecimal calculateRndDeduction(CompanyFinancialData data) {
        // 模拟研发费用计算，实际中会更复杂
        return data.getDeductibleExpenses().multiply(new BigDecimal("0.2")) // 假设20%是研发费用
                .multiply(new BigDecimal("0.75")); // 加计75%扣除
    }

    @Override
    public boolean isApplicable(CompanyFinancialData data) {
        return "HIGH_TECH_ENTERPRISE".equals(data.getCompanyType());
    }
}
